Export To Excel

This is a simple example of how to export data to Excel. Say that you have a list with your data called “mydata”. Then the following script will export that into column A in a new Excel document and then save it. Uncomment the last two rows to close the sheet and Excel.

Transparent

mydata = [1,2,3,4,5,6,7,8,9,10]

import clr
clr.AddReference("Microsoft.Office.Interop.Excel") 
import Microsoft.Office.Interop.Excel as Excel
excel = Excel.ApplicationClass()
wb = excel.Workbooks.Add()
excel.Visible = True
ws = wb.Worksheets(1)
# Write data to column A
for index, value in enumerate(mydata, start=1):
    ws.Cells(index, 1).Value = value
# Save the workbook
wb.SaveAs(r'C:\Temp\MyExcelTest.xlsx')
# Close the workbook and quit Excel
#wb.Close()
#excel.Quit()